package com.csqf.booksys.controller;

import java.util.List;
import java.util.Scanner;

import com.csqf.booksys.entity.Book;
import com.csqf.booksys.service.IBookService;
import com.csqf.booksys.service.impl.BookServiceImpl;
import com.csqf.booksys.utils.DateUtils;

/**
 * @author enbo.xie
 * @Date 2020年8月12日 下午4:47:19
 */
public class BookController {
	static Scanner scan = new Scanner(System.in);
	static IBookService bookServiceImpl = new BookServiceImpl();
	public static void main(String[] args) {
		while(true){
			int num = showMenu();
			switch(num){
			case 1:
				addBook();
				break;
			case 2:
				addBook();
				break;
			case 3:
				addBook();
				break;
			case 4:
				addBook();
				break;
			case 5:
				findBookByName();
				break;
			case 6:
				findAllBook();
				break;
			case 0:
				System.out.println("谢谢你的使用");
				System.exit(0);
			}
		}
	}
	
	public static int showMenu(){
		System.out.println("--------图书管理系统--------");
		System.out.println("--------1、添加图书--------");
		System.out.println("--------2、更新图书--------");
		System.out.println("--------3、删除图书--------");
		System.out.println("--------4、按ID查询图书--------");
		System.out.println("--------5、按书名查询图书--------");
		System.out.println("--------6、查询全部图书--------");
		System.out.println("--------0、退出系统--------");
		System.out.print("请输入你要执行的操作：");
		int num = scan.nextInt();
		return num;
	}
	
	public static void addBook(){
		System.out.print("请输入图书的名称：");
		String name = scan.next();
		System.out.print("请输入图书的作者：");
		String author = scan.next();
		System.out.print("请输入图书的出版社：");
		String publish = scan.next();
		System.out.print("请输入图书的价格：");
		String price = scan.next();
		System.out.print("请输入图书的发布时间（yyyy-MM-dd）：");
		String crtTime = scan.next();
		//封装
		Book book = new Book();
		book.setName(name);
		book.setAuthor(author);
		book.setPublish(publish);
		book.setPrice(Double.parseDouble(price));
		book.setCrtTime(DateUtils.strToUtilDate(crtTime));
		//调用业务层
		int flag = bookServiceImpl.addBook(book);
		if(flag > 0){
			System.out.println("添加成功！");
		}else{
			System.out.println("添加失败！");
		}
	}
	
	public static void findAllBook(){
		List<Book> list = bookServiceImpl.findAll();
		list.stream().forEach(System.out::println);
	}
	
	public static void findBookByName(){
		System.out.println("请输入书名：");
		String name = scan.next();
		List<Book> list = bookServiceImpl.findByName(name);
		list.stream().forEach(System.out::println);
	}
	
	
	

}
